package com.SouthernWall_404.AgeSuitcase.Common.world.item;

import com.SouthernWall_404.AgeSuitcase.Common.API.GeneralAPI;
import com.SouthernWall_404.AgeSuitcase.Common.API.Info.FileInfo;
import com.SouthernWall_404.AgeSuitcase.Common.AgeSuitcase;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

public class FileItem extends Item {

    public static String FILE_TAG="file";
    public FileItem(Properties p_41383_) {
        super(p_41383_);
    }


    @Override
    public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {

        String Date= GeneralAPI.getDate();
        String Code=GeneralAPI.getCode();
        ItemStack itemStack=player.getItemInHand(hand);

        if(getFile(itemStack).code.isEmpty()||getFile(itemStack).date.isEmpty())
        {
            setFile(itemStack,new FileInfo(Date,Code));
        }


        return super.use(level, player, hand);
    }

//    public static void setFilePack(ItemStack itemStack,)

    public static CompoundTag getOrCreateModPack(ItemStack itemStack)
    {
        CompoundTag nbt=itemStack.getOrCreateTag();
        CompoundTag modPack=new CompoundTag();
        if(nbt.contains(AgeSuitcase.MODID))
        {
            modPack=nbt.getCompound(AgeSuitcase.MODID);
        }

        return modPack;
    }

    public static void setFile(ItemStack itemStack,FileInfo fileInfo)
    {
        CompoundTag modPack=getOrCreateModPack(itemStack);
        CompoundTag filePack=fileInfo.getFilePack();
        modPack.put(FILE_TAG,filePack);
    }

    public static FileInfo getFile(ItemStack itemStack)
    {
        FileInfo fileInfo=new FileInfo(itemStack);
        return fileInfo;
    }
}
